﻿var g_nLanguage = 0;
function setEnVersion() {
  g_nLanguage = 1;
}
function getUrlParam(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); //构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg); //匹配目标参数
  if (r != null) return unescape(r[2]);
  return ''; //返回参数值
}
function setcookie(name, value) {
  var Days = 1;
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString() + ';path=/';
}

function getcookie(name) {
  var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
  if (arr != null) {
    return unescape(arr[2]);
  } else {
    return '';
  }
}
function showLoader() {
  //显示加载器.for jQuery Mobile 1.1.0
  $.mobile.loadingMessage = '加载中...'; //显示的文字
  $.mobile.loadingMessageTextVisible = true; //是否显示文字
  $.mobile.loadingMessageTheme = 'a'; //加载器主题样式a-e
  $.mobile.showPageLoadingMsg(); //显示加载器
}
function changeTwoDecimal(x) {
  var f_x = parseFloat(x);
  if (isNaN(f_x)) {
    alert('function:changeTwoDecimal->parameter error');
    return false;
  }
  var f_x = Math.round(x * 100) / 100;

  return f_x;
}

//隐藏加载器.for jQuery Mobile 1.1.0
function hideLoader() {
  //隐藏加载器
  $.mobile.hidePageLoadingMsg();
}
function isAuthFailed(ret) {
  if (ret == -100) {
    return true;
  }
  return false;
}
function toLogin() {
  var cururl = window.location.href;
  cururl = cururl.replace('sessionkey=', 'sxxxx=');
  cururl = cururl.replace('userid=', 'uxxxx=');
  cururl = cururl.replace('&', '%26');
  var loginurl = '/joygo/login.html?check=' + cururl;
  if (g_nLanguage == 1) {
    loginurl = '/joygoen/login.html?check=' + cururl;
  }
  window.open(loginurl, '_self');
}
function GetWinRaio(wincount, losecount) {
  if (wincount + losecount == 0) {
    return 0;
  }
  var r = (wincount * 100) / (wincount + losecount);
  return r.toFixed(2);
}
function GetLevelString(nScore) {
  var levelstr;
  if (nScore >= 1000) {
    var s = Math.floor((nScore - 900) / 100);
    levelstr = '9.' + s;
    if (g_nLanguage == 0) {
      levelstr += '段';
    } else {
      levelstr += 'D';
    }
  } else if (nScore >= 100) {
    levelstr = Math.floor(nScore / 100);
    if (g_nLanguage == 0) {
      levelstr += '段';
    } else {
      levelstr += 'D';
    }
  } else {
    levelstr = Math.floor(Math.abs(nScore - 199) / 100);
    if (g_nLanguage == 0) {
      levelstr += '级';
    } else {
      levelstr += 'K';
    }
  }
  return levelstr;
}

function joygo_Alert(str) {
  var msgw, msgh, bordercolor;
  msgw = 300; //Width
  if (str.length < 30) {
    msgh = 100; //Height
  } else if (str.length < 100) {
    msgh = 150; //Height
  } else if (str.length < 200) {
    msgh = 200;
  } else if (str.length < 400) {
    msgh = 400;
  } else {
    msgh = 600;
  }

  titleheight = 25; //title Height
  bordercolor = '#336699'; //boder color
  titlecolor = '#99CCFF'; //title color

  var sWidth, sHeight;
  sWidth = document.body.offsetWidth;
  sHeight = screen.height;
  var bgObj = document.createElement('div');
  bgObj.setAttribute('id', 'bgDiv');
  bgObj.style.position = 'absolute';
  bgObj.style.top = '0';
  bgObj.style.background = '#777';
  bgObj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75';
  bgObj.style.opacity = '0.6';
  bgObj.style.left = '0';
  bgObj.style.width = sWidth + 'px';
  bgObj.style.height = sHeight + 'px';
  bgObj.style.zIndex = '10000';
  document.body.appendChild(bgObj);

  var msgObj = document.createElement('div');
  msgObj.setAttribute('id', 'msgDiv');
  msgObj.setAttribute('align', 'center');
  msgObj.style.background = 'white';
  msgObj.style.border = '1px solid ' + bordercolor;
  msgObj.style.position = 'fixed';
  msgObj.style.left = '50%';
  msgObj.style.top = '50%';
  msgObj.style.font = '12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif';
  msgObj.style.marginLeft = '-150px';
  if (str.length > 200) {
    msgObj.style.marginTop = -200 + document.documentElement.scrollTop + 'px';
  } else {
    msgObj.style.marginTop = -75 + document.documentElement.scrollTop + 'px';
  }

  msgObj.style.width = msgw + 'px';
  msgObj.style.height = msgh + 'px';
  msgObj.style.textAlign = 'center';
  msgObj.style.lineHeight = '25px';
  msgObj.style.zIndex = '10001';

  var title = document.createElement('h4');
  title.setAttribute('id', 'msgTitle');
  title.setAttribute('align', 'right');
  title.style.margin = '0';
  title.style.padding = '3px';
  title.style.background = bordercolor;
  title.style.filter =
    'progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);';
  title.style.opacity = '0.75';
  title.style.border = '1px solid ' + bordercolor;
  title.style.height = '18px';
  title.style.font = '12px Verdana, Geneva, Arial, Helvetica, sans-serif';
  title.style.color = 'white';
  title.style.cursor = 'pointer';
  title.innerHTML = '关闭';
  title.onclick = function () {
    document.body.removeChild(bgObj);
    document.getElementById('msgDiv').removeChild(title);
    document.body.removeChild(msgObj);
  };
  document.body.appendChild(msgObj);
  document.getElementById('msgDiv').appendChild(title);
  var txt = document.createElement('p');
  txt.style.margin = '1em 0';
  txt.setAttribute('id', 'msgTxt');
  txt.innerHTML = str;
  document.getElementById('msgDiv').appendChild(txt);
}

function clickToUserCenter(userid) {
  var url = '/joygo/usercenter.html?getuserid=';
  url += userid;
  window.open(url, '_self');
}
function timestamptolocaltime(timestamp) {
  var newDate = new Date();
  newDate.setTime(timestamp);
  return newDate.toLocaleDateString().replace(/\//g, '-') + ' ' + newDate.toTimeString().substr(0, 8);
}
